// pages/rules/rules.js
const wxapi = require('../../utils/wxapi.js');
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // articleList:[],
    statusType: [],
    currentType: 0,
    list: [],
    goodsMap: [],
    logisticsMap: [],
    windowHeight: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getArticles();
    let systemInfo = wx.getSystemInfoSync()
    this.setData({
      windowHeight: systemInfo.windowHeight,
      currentType: options.id ? options.id : 0
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTypes();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  gotoDetail(e){
      wx.navigateTo({
        url: '/pages/rules/rulesDetail/rulesDetail?id=' + e.currentTarget.dataset.id,
      })
  },
  getArticles(){
    wxapi.getArticles(wx.getStorageSync('secKey')).then(data => {
      let newData = data.data.map((item) => {
        item.createtime = util.formatTime(new Date(item.createtime / 1000))
        return item;
      })
      that.setData({ list: newData })
    }) 
  },
  getTypes(){
    let that = this;
    let key = wx.getStorageSync("secKey");
    wxapi.getTypes(key, 0).then(res => {
      that.setData({
        statusType: res.data
      })
      return res.data;
    }).then((types) => {
      return types.length && wxapi.getTypesArticlesInRules(key, types);
    }).then(rtnData => {
      let colData = rtnData.map(item => {
        item.data.forEach(i => {
          i.createtime = util.formatTime(new Date(i.createtime / 1000));
          i.posterUrl = i.posterUrl == "" ? "../../static/image/other-selected.png" : i.posterUrl;
        })
        return item.data;
      })
      that.setData({
        list: colData
      })
    })
  },
  swichNav: function (res) {
    if (this.data.currentType == res.detail.currentNum) return;
    this.setData({
      currentType: res.detail.currentNum
    })
  },
  bindChange: function (e) {
    this.setData({
      currentType: e.detail.current
    })
    if (!this.data.list[e.detail.current].length){}
      // this.getList();  讲道理这里是需要再请求一次数据的
  },
})